package flowcontrol.countdownlatch;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Classname CountDownLatchDemo1
 * @Description 一等多 工厂中，质检，5个工人检查，所有人都认为通过，才通过
 * @Date 2025/1/11 18:26
 * @Author zhuzhenguo
 */
public class CountDownLatchDemo1 {

    public static void main(String[] args) throws InterruptedException {
        // 需要等待的次数
        CountDownLatch latch = new CountDownLatch(5);
        ExecutorService service = Executors.newFixedThreadPool(5);
        for (int i = 0; i < 5; i++) {
            final int no = i + 1;
            Runnable runnable = () -> {
                try {
                    Thread.sleep((long) (Math.random() * 10000));
                    System.out.println("No." + no + "完成了检查。");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } finally {
                    // 谁来-1谁来调用countDown
                    latch.countDown();
                }
            };
            service.submit(runnable);
        }
        System.out.println("等待5个人检查完.....");
        // 让谁等待谁就调用await方法
        latch.await();
        System.out.println("所有人都完成了工作，进入下一个环节。");
    }

}
